package com.example.workbench.common.vo;

import com.github.pagehelper.PageInfo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


public class BasePageVO<T> implements Serializable {

    public static final Long defaultPageNum = 1L;
    public static final Long defaultPageSize = 10L;
    public static final List defaultEmptyList = new ArrayList();
    public static final Long defaultTotalNum = 0L;
    public static final Long defaultTotalPages = 0L;

    private static final long serialVersionUID = 7299837643024323482L;
    private Long curPage;

    private Long size;

    private Long totalPage;

    private Long total;

    private List<T> data;

    public BasePageVO(PageInfo<T> page) {
        this.total = page.getTotal();
        this.totalPage = (long)page.getPages();
        this.data = page.getList();
        this.size = (long)page.getPageSize();
        this.curPage = (long)page.getPageNum();
    }

    public BasePageVO(PageInfo page, List<T> list) {
        this.total = page.getTotal();
        this.totalPage = (long)page.getPages();
        this.data = list;
        this.size = (long)page.getPageSize();
        this.curPage = (long)page.getPageNum();
    }

    public BasePageVO() {
        this.curPage = defaultPageNum;
        this.size = defaultPageSize;
        this.data = defaultEmptyList;
        this.totalPage = defaultTotalPages;
        this.total = defaultTotalNum;
    }

    public BasePageVO(PageInfo page, List<T> list, Long total, boolean calculatePage) {
        if (calculatePage) {
            this.total = total;
            this.totalPage = total % (long)page.getPageSize() == 0L ? total / (long)page.getPageSize() : total / (long)page.getPageSize() + 1L;
            this.data = list;
            this.size = (long)page.getPageSize();
            this.curPage = (long)page.getPageNum();
        } else {
            this.total = page.getTotal();
            this.totalPage = (long)page.getPages();
            this.data = list;
            this.size = (long)page.getPageSize();
            this.curPage = (long)page.getPageNum();
        }

    }

    public BasePageVO(PageInfo<T> page, Long total, boolean calculatePage) {
        if (calculatePage) {
            this.total = total;
            this.totalPage = total % (long)page.getPageSize() == 0L ? total / (long)page.getPageSize() : total / (long)page.getPageSize() + 1L;
            this.data = page.getList();
            this.size = (long)page.getPageSize();
            this.curPage = (long)page.getPageNum();
        } else {
            this.total = page.getTotal();
            this.totalPage = (long)page.getPages();
            this.data = page.getList();
            this.size = (long)page.getPageSize();
            this.curPage = (long)page.getPageNum();
        }

    }

    public static BasePageVO build(PageInfo<Object> page) {
        return new BasePageVO(page);
    }

    public static BasePageVO build(List<Object> list, PageInfo pageInfo) {
        return new BasePageVO(pageInfo, list);
    }

    public static BasePageVO buildEmpty() {
        return new BasePageVO();
    }

    public static  BasePageVO build(List<Object> list, PageInfo pageInfo, Long total, boolean calculatePage) {
        return new BasePageVO(pageInfo, list, total, calculatePage);
    }

    public static BasePageVO build(PageInfo<Object> page, Long total, boolean calculatePage) {
        return new BasePageVO(page, total, calculatePage);
    }

    public void setCurPage(Long curPage) {
        this.curPage = curPage;
    }

    public void setSize(Long size) {
        this.size = size;
    }

    public void setTotalPage(Long totalPage) {
        this.totalPage = totalPage;
    }

    public void setTotal(Long total) {
        this.total = total;
    }

    public void setData(List<T> data) {
        this.data = data;
    }

    public Long getCurPage() {
        return this.curPage;
    }

    public Long getSize() {
        return this.size;
    }

    public Long getTotalPage() {
        return this.totalPage;
    }

    public Long getTotal() {
        return this.total;
    }

    public List<T> getData() {
        return this.data;
    }
}
